package com.example.day02;

import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private TextView mText;
    private Toolbar mToob;
    private FrameLayout mFrag;
    private TabLayout mTab;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mText = (TextView) findViewById(R.id.text);
        mToob = (Toolbar) findViewById(R.id.toob);
        mFrag = (FrameLayout) findViewById(R.id.frag);
        mTab = (TabLayout) findViewById(R.id.tab);

        mToob.setTitle("");
        setSupportActionBar(mToob);

        final AlankFragment alankFragment = new AlankFragment();
        final BlankFragment blankFragment = new BlankFragment();
        mTab.addTab(mTab.newTab().setText("首页"));
        mTab.addTab(mTab.newTab().setText("我的"));

        manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.frag,alankFragment).add(R.id.frag,blankFragment).show(alankFragment).hide(blankFragment).commit();

        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                switch (tab.getPosition()){

                    case 0:
                        mText.setText("首页");
                        manager.beginTransaction().show(alankFragment).hide(blankFragment).commit();
                    break;
                    case 1:
                        mText.setText("我的");
                        manager.beginTransaction().show(blankFragment).hide(alankFragment).commit();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });







    }
}
